package org.dwell.vertx3.demos.core;

import io.vertx.core.Vertx;

/**
 * Created by JianGuo on 2016/1/14.
 */
public class VertxDemo {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.setPeriodic(1000, id ->{
            //This handler will get called every second.
            System.out.println("timer fired!");
        });

        //如果你需要执行同步的代码，请使用下面的方式，这是Vert.x推荐使用的，不会阻塞Event Loop
        //另一种方式是使用worker verticle，它始终从worker pool中取得一个工作线程取执行
        vertx.executeBlocking(future -> {
            //Call some blocking API that takes a significant amount of time to return
            String result = blockMe();
            future.complete(result);
        },res -> {
            System.out.println("The result is: " + res.result());
        });


    }


    private static String blockMe() {
        try {
            Thread.sleep(4000);
            return "yes,i'm back!";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }
}
